package core;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

public class OpenedFile {

	private List<String> _records;
	private static OpenedFile _self = null;
	
	public static OpenedFile getInstance(String filename) {
		if (_self == null) {
			_self = new OpenedFile(filename);
		}
		return _self;
	}
	
	public static OpenedFile getInstance() {
		if (_self == null) {
			_self = new OpenedFile();
		}
		return _self;
	}
	
	private OpenedFile() {

	}
	
	private OpenedFile(String filename) {
		read(filename);
	}
	
	public void setNewFile(String filename) {
		read(filename);
	}
	
	private void read(String filename) {
		try {
			_records = new ArrayList<String>();
			BufferedReader reader = new BufferedReader(new FileReader(filename));
			String line;
			while ((line = reader.readLine()) != null) {
				_records.add(line);
			}
			reader.close();
		} catch (Exception e) {
			System.err.format("Exception occurred trying to read '%s'.",
					filename);
			e.printStackTrace();
		}
	}
	
	public List<String> getFile() {
		return _records;
	}
	
	public String getStringFormat() {
		StringBuffer ret = new StringBuffer("");
		for (String line : _records) {
			ret.append(line).append("\n");
		}
		return ret.toString();
	}
	
	public String toString() {
		StringBuffer ret = new StringBuffer("");
		for (String line : _records) {
			ret.append(line).append("\n");
		}
		return ret.toString();
	}
}
